﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF MAPPED PAGE PROVIDER FOR EPISERVER CMS.
MAPPED PAGE PROVIDER FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

MAPPED PAGE PROVIDER FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH MAPPED PAGE PROVIDER FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Core;
using EPiServer.DataAbstraction;


namespace EPiServer.Research.PageProviders
{
    public abstract class MappedPageProvider : PageProviderBase
    {

        /// <summary>
        /// Return a list of string keys to children from the root of the provider
        /// </summary>
        /// <returns></returns>
        protected abstract List<string> GetRootChildren();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        protected abstract List<string> GetChildren(string Key);


        protected override PageReferenceCollection GetChildrenReferences(PageReference pageLink, string languageID)
        {
            if (pageLink.CompareToIgnoreWorkID(EntryPoint))
            {
                return new PageReferenceCollection(GetRootChildren().Select(s => base.ConstructPageReference(MappedPageStore.Instance.LoadMapping(Name,s).PageID)).ToArray());
            }
            else
            {
                var l = GetChildren(MappedPageStore.Instance.LoadMapping(Name,pageLink.ID).PageKey);
                if (l == null) return new PageReferenceCollection();
                return new PageReferenceCollection(l.Select(s => base.ConstructPageReference(MappedPageStore.Instance.LoadMapping(Name,s).PageID)).ToArray());
            }
        }

        protected abstract PageData GetPage(string Key);

        protected override PageData GetLocalPage(PageReference pageLink, ILanguageSelector languageSelector)
        {
            return GetPage(MappedPageStore.Instance.LoadMapping(Name,pageLink.ID).PageKey);
        }


        protected void InitializePageData(PageData pd, string Key, string PageName, string PageTypeName, string Parent)
        {
            MappedPage pm = MappedPageStore.Instance.LoadMapping(Name,Key);
            PageReference pr = (Parent == null) ? EntryPoint : base.ConstructPageReference(MappedPageStore.Instance.LoadMapping(Name,Parent).PageID);
            base.InitializePageData(pd, PageName, PageTypeName, pm.PageGuid, base.ConstructPageReference(pm.PageID), pr, new List<string>());
        }

        protected void InitializePageData(PageData pd, string Key, string PageName, string Parent)
        {
            InitializePageData(pd, Key, PageName, GetPageType(Key), Parent);
        }

        protected abstract string GetPageType(string Key);

        protected override Uri ResolveLocalPage(PageReference pageLink, out Guid guid)
        {
            if (pageLink.RemoteSite != this.Name)
            {
                guid = Guid.Empty;
                return null;
            }
            MappedPage ppm = MappedPageStore.Instance.LoadMapping(Name,pageLink.ID);
            guid = ppm.PageGuid;
            return base.ConstructPageUri(PageType.Load(GetPageType(ppm.PageKey)).ID, pageLink);
        }

        protected override Uri ResolveLocalPage(Guid pageGuid, out PageReference pageLink)
        {
            if (MappedPageStore.Instance.LoadMapping(Name,pageGuid)==null)
            {
                pageLink = PageReference.EmptyReference;
                return null;
            }
            else
            {
                var ppm = MappedPageStore.Instance.LoadMapping(Name,pageGuid);
                pageLink = base.ConstructPageReference(ppm.PageID);
                return base.ConstructPageUri(PageType.Load(GetPageType(ppm.PageKey)).ID, pageLink);
            }
        }

        public string ConvertPageReference(PageReference pr)
        {
            if (pr.CompareToIgnoreWorkID(EntryPoint)) return null;
            return GetMapping(pr.ID).PageKey;
        }

        public PageReference ConvertKey(string Key)
        {
            if (Key == null) return EntryPoint;
            return base.ConstructPageReference(GetMapping(Key).PageID);
        }


        public virtual void Move(string pageKey, string destinationKey)
        {
            throw (new NotImplementedException());
        }

        public override void Move(PageReference pageLink, PageReference destinationLink)
        {
            Move(ConvertPageReference(pageLink), ConvertPageReference(destinationLink));
        }

        public virtual string SaveNew(PageData page, string ParentKey, EPiServer.DataAccess.SaveAction action) {
            throw (new NotImplementedException());
        }

        public virtual void Save(PageData page, string Key, EPiServer.DataAccess.SaveAction action)
        {
            throw (new NotImplementedException());
        }

        public override PageReference Save(PageData page, EPiServer.DataAccess.SaveAction action)
        {
            if (page.PageLink == PageReference.EmptyReference)
            {
                //New
                return ConvertKey(SaveNew(page, ConvertPageReference(page.ParentLink), action));
            }
            else
            {
                Save(page, ConvertPageReference(page.PageLink), action);
                return page.PageLink;
            }
        }

        protected virtual MappedPage GetMapping(Guid g)
        {
            return MappedPageStore.Instance.LoadMapping(Name, g);
        }

        protected virtual MappedPage GetMapping(string Key)
        {
            return MappedPageStore.Instance.LoadMapping(Name, Key);
        }

        protected virtual MappedPage GetMapping(int id)
        {
            return MappedPageStore.Instance.LoadMapping(Name, id);
        }

    }
}
